

#import "FAMBMIViewController.h"

@interface FAMBMIViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *FAMPageBackView;

@property (weak, nonatomic) IBOutlet UILabel *FAMWeightLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMNormLabel;
@property (weak, nonatomic) IBOutlet UIButton *FAMIndicateButton;
@property (weak, nonatomic) IBOutlet UIView *FAMProgressView;
@property (weak, nonatomic) IBOutlet UIView *FAMBMIBackIView;


@property (weak, nonatomic) IBOutlet UIImageView *FAMOneImageView;
@property (weak, nonatomic) IBOutlet UIImageView *FAMTwoImageView;
@property (weak, nonatomic) IBOutlet UIImageView *FAMThreeImageView;
@property (weak, nonatomic) IBOutlet UIImageView *FAMFourImageView;

@property (weak, nonatomic) IBOutlet UILabel *FAMOneNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMTwoNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMThreeNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMFourNameLabel;

@property (weak, nonatomic) IBOutlet UILabel *FAMOneCalLabel;

@property (weak, nonatomic) IBOutlet UILabel *FAMTwoCalLabel;

@property (weak, nonatomic) IBOutlet UILabel *FAMThreeCalLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMFourCalLabel;
@property (weak, nonatomic) IBOutlet UILabel *FAMHintLabel;

@property (weak, nonatomic) IBOutlet UIView *FAMBlueView;
@property (weak, nonatomic) IBOutlet UIView *FAMGreenView;
@property (weak, nonatomic) IBOutlet UIView *FAMYellowView;
@property (weak, nonatomic) IBOutlet UIView *FAMOrangeView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *FAMFAMBlueViewX;
@property (weak, nonatomic) IBOutlet UIScrollView *FAMScrollView;

@property (weak, nonatomic) IBOutlet UITextView *FAMPolicyTextView;

@end

@implementation FAMBMIViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.fd_prefersNavigationBarHidden = YES;
    [self FAM_showUi];
}
- (void)FAM_showUi{
    self.FAMScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    float weight = ((NSString *)self.FAMFoodmateUserdataDictionary[@"weight"]).floatValue;
    float height = ((NSString *)self.FAMFoodmateUserdataDictionary[@"height"]).floatValue / 100;
    float bmi = weight/(height * height);
    self.FAMWeightLabel.text = [NSString stringWithFormat:@"%0.2f",bmi];
//    CGRect progressViewFrame = self.FAMProgressView.frame;
//    CGFloat y = bmi/100 * progressViewFrame.size.width - 60;
//    if (y >= progressViewFrame.size.width) {
//        y = progressViewFrame.size.width - 30;
//    }
//    self.FAMIndicateButton.sd_layout.leftSpaceToView(self.FAMBMIBackIView, y).centerYEqualToView(self.FAMProgressView);
    // bmi<18 偏瘦，18<bmi<24正常，24<bmi<28 超重 bmi>28肥胖
    if (bmi < 18) {
        self.FAMNormLabel.text = @"Lighter";
        self.FAMNormLabel.textColor = kCustom0xColor(0x33D078, 1.0);
    }else if (bmi>=18 && bmi < 24){
        self.FAMNormLabel.text = @"suitable";
        self.FAMNormLabel.textColor = kCustom0xColor(0xFFA704, 1.0);
    }else if (bmi >= 24 ){
        self.FAMNormLabel.text = @"overweight";
        self.FAMNormLabel.textColor = kCustom0xColor(0xFF5324, 1.0);
    }
    if (bmi <= 18.4) {
        CGFloat x = self.FAMBlueView.frame.size.width / 18.4;
        self.FAMFAMBlueViewX.constant = x * bmi;
    }else if (bmi>18.4 && bmi <= 23.9){
        CGFloat x = (self.FAMGreenView.frame.size.width / 5.5) * (bmi - 18.4) + self.FAMBlueView.frame.size.width;
        self.FAMFAMBlueViewX.constant = x;
    }else if (bmi>23.9 && bmi <=27.9){
        self.FAMIndicateButton.sd_layout.leftSpaceToView(self.FAMBMIBackIView,  self.FAMBlueView.frame.size.width + self.FAMGreenView.frame.size.width + ((self.FAMYellowView.frame.size.width/4.0) * (bmi - 23.9)));
        self.FAMFAMBlueViewX.constant = self.FAMBlueView.frame.size.width + self.FAMGreenView.frame.size.width + 44 + ((self.FAMYellowView.frame.size.width/4.0) * (bmi - 23.9));
    }else{
        self.FAMFAMBlueViewX.constant = self.FAMBlueView.frame.size.width + self.FAMGreenView.frame.size.width + self.FAMYellowView.frame.size.width + ((self.FAMOrangeView.frame.size.width/12.1) * (bmi - 27.9));
    }
    WeakSelf(self);
    self.FAMPageBackView.layer.shadowColor = kCustom0xColor(0x000000, 1.0).CGColor;
    self.FAMPageBackView.layer.shadowOpacity = 0.15;
    self.FAMPageBackView.layer.shadowRadius = 1.5;
    self.FAMPageBackView.layer.shadowOffset = CGSizeMake(2, 2);
    [self showHudInKeyWindowWithHint:@""];
    [FAMHttpTool FAM_GetFoodmateSuggestWithParameters:nil targetType:self.FAMFoodmateUserdataDictionary[@"target_type"] success:^(FAMFoodmateUserdataModel * _Nonnull model, NSString * _Nonnull message) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            [weakSelf FAM_fillUi:model];
        });
    } failure:^(NSString * _Nonnull errorInfo) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [weakSelf hideHudKeyWindow];
            [weakSelf showHint:errorInfo];
        });
    }];
    
//    NSMutableAttributedString *attributedString = [FAMTool FAM_AttributedString:@"High BMI brings greater health risks," styleTwoString:@"Learn more" styleColorOne:kWhiteColor styleColorTwo:kWhiteColor styleFontOne:12 styleFontTwo:12];
//    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(attributedString.length - 10, 10)];
//    self.FAMHintLabel.attributedText = attributedString;
    
    self.FAMPolicyTextView.delegate = self;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithAttributedString:self.FAMPolicyTextView.attributedText];
    [attributedString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:NSMakeRange(0, attributedString.length)];
    [attributedString addAttribute:NSLinkAttributeName
                                 value:@"healthline://"
                                 range:[[attributedString string] rangeOfString:@"· Basal metabolic rate"]];
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"health://"
                             range:[[attributedString string] rangeOfString:@". Calorie counting-Harvard"]];
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"pubmed://"
                             range:[[attributedString string] rangeOfString:@". International Society of Sports Nutrition"]];
    [attributedString addAttribute:NSLinkAttributeName
                             value:@"nhlbi://"
                             range:[[attributedString string] rangeOfString:@". National Institutes of Health"]];
    self.FAMPolicyTextView.attributedText = attributedString;
    self.FAMPolicyTextView.editable = NO;
    self.FAMPolicyTextView.scrollEnabled = NO;
    
}
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([[URL scheme] isEqualToString:@"healthline"]) {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://www.healthline.com/health/what-is-basal-metabolic-rate"] options:@{} completionHandler:nil];
        return NO;
    } else if ([[URL scheme] isEqualToString:@"health"]) {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://www.health.harvard.edu/staying-healthy/calorie-counting-made-easy"] options:@{} completionHandler:nil];
        return NO;
    }else if ([[URL scheme] isEqualToString:@"pubmed"]) {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://pubmed.ncbi.nlm.nih.gov/28630601/"] options:@{} completionHandler:nil];
        return NO;
    }else if ([[URL scheme] isEqualToString:@"nhlbi"]) {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"https://www.nhlbi.nih.gov/files/docs/guidelines/ob_gdlns.pdf"] options:@{} completionHandler:nil];
        return NO;
    }
    return YES;
}
- (void)FAM_fillUi:(FAMFoodmateUserdataModel *)model{
    [self.FAMOneImageView sd_setImageWithURL:((FAMFoodmateUserdataModel *)model.suggestList[0]).image_url placeholderImage:[UIImage imageNamed:@"general_default_icon"]];
    self.FAMOneNameLabel.text = ((FAMFoodmateUserdataModel *)model.suggestList[0]).name;
    [self.FAMTwoImageView sd_setImageWithURL:((FAMFoodmateUserdataModel *)model.suggestList[1]).image_url placeholderImage:[UIImage imageNamed:@"general_default_icon"]];
    self.FAMTwoNameLabel.text = ((FAMFoodmateUserdataModel *)model.suggestList[1]).name;
    [self.FAMThreeImageView sd_setImageWithURL:((FAMFoodmateUserdataModel *)model.suggestList[2]).image_url placeholderImage:[UIImage imageNamed:@"general_default_icon"]];
    self.FAMThreeNameLabel.text = ((FAMFoodmateUserdataModel *)model.suggestList[2]).name;
    [self.FAMFourImageView sd_setImageWithURL:((FAMFoodmateUserdataModel *)model.suggestList[3]).image_url placeholderImage:[UIImage imageNamed:@"general_default_icon"]];
    self.FAMFourNameLabel.text = ((FAMFoodmateUserdataModel *)model.suggestList[3]).name;
    CGFloat total = 0;
    float weight = ((NSString *)self.FAMFoodmateUserdataDictionary[@"weight"]).floatValue;
    float height = ((NSString *)self.FAMFoodmateUserdataDictionary[@"height"]).floatValue;
    int age = ((NSString *)self.FAMFoodmateUserdataDictionary[@"age"]).intValue;
    if ([self.FAMFoodmateUserdataDictionary[@"gender"] isEqualToString:@"1"]) {
        total = 67 + 13.73 * weight + 5 * height - 6.9 / age;
    }else{
        total = 61 + 9.6 * weight + 1.72 * height - 4.7 / age;
    }
    if ([self.FAMFoodmateUserdataDictionary[@"target_type"] isEqualToString:@"1"]) {
        total -= 200;
    }else if ([self.FAMFoodmateUserdataDictionary[@"target_type"] isEqualToString:@"2"]){
        
    }else{
        total += 200;
    }
    self.FAMOneCalLabel.text = [NSString stringWithFormat:@"%0.2f cal",total * 0.2];
    self.FAMTwoCalLabel.text = [NSString stringWithFormat:@"%0.2f cal",total * 0.35];
    self.FAMThreeCalLabel.text = [NSString stringWithFormat:@"%0.2f cal",total * 0.25];
    self.FAMFourCalLabel.text = [NSString stringWithFormat:@"%0.2f cal",total * 0.20];
}
- (IBAction)FAM_backButtonClick:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)FAM_nextButtonClick:(UIButton *)sender {
    [UIApplication sharedApplication].delegate.window.rootViewController = [FAMTabBarViewController new];
    [FAMStaticTool FAM_setUserDefaultsValue:@(YES) key:kIsFirst];
}


@end
